IF EXISTS(SELECT name FROM sysobjects
      WHERE name = 'pOutProdPaper_Get' AND type = 'P')
   DROP PROCEDURE pOutProdPaper_Get
GO

CREATE PROCEDURE dbo.pOutProdPaper_Get
	@vdatFromDate DATETIME = NULL,
	@vdatToDate DATETIME = NULL
AS


/*______________________ CONSTANT DECLARATIONS _________________________*/

  

/*______________________ VARIABLE DECLARATIONS _________________________*/

  
/*_______________________________ INIT _________________________________*/


/*________________________ VALIDATE PARAMETERS _________________________*/
	
/*_______________________________ MAIN _________________________________*/
	
	
	SELECT 
		OutProdPaper.Id, 
		OutProdPaper.[Date],
		TotalAmount = (SELECT SUM(Amount) FROM OutProdDetail
						WHERE OutProdPaperId = OutProdPaper.Id),
		OutProdPaper.ToShopId,
		RowNumber = ROW_NUMBER() OVER(ORDER BY OutProdPaper.Date DESC)
	 FROM 
		OutProdPaper
		
	WHERE ((@vdatFromDate IS NULL) OR (DATEDIFF(DAY, @vdatFromDate, OutProdPaper.Date) >= 0))
		AND ((@vdatToDate IS NULL) OR (DATEDIFF(DAY, OutProdPaper.Date, @vdatToDate) >= 0))
		
	ORDER BY OutProdPaper.Date DESC

/*___________________________ ERROR HANDLER ____________________________*/



